//
//  VCSearch.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/22.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCSearch: VCBase {
    
    // 头部背景图片
    private lazy var topImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "background_search"))
        view.contentMode = .scaleToFill
        view.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT * 0.3)
        return view
    }()
    
    // 切换按钮
    private lazy var segmentSearchView: SegmentSearchView = {
        let ui = SegmentSearchView()
        ui.delegate = self
        let uiY = topImg.frame.maxY - MARGIN50
        ui.frame = CGRect(x: MARGIN, y: uiY, width: SCREEN_WIDTH - MARGIN * 2, height: SCREEN_HEIGHT * 0.7)
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_views()
        init_event()
    }
}

// MARK: 逻辑部分
extension VCSearch {
    
    func searchFilghts(items:NSDictionary) -> Void {
        // 跳转界面
        let vc = VCShowTicket()
        vc.requestDict = items
        vc.title = "\(items["city"]!) - \(items["endcity"]!)"
        self.pushNextVcHideBottomBar(vc: vc)
    }
}

extension VCSearch {
    
    // TODO 查票功能
    private func init_event() -> Void {
        segmentSearchView.complete = { dict in
            self.searchFilghts(items: dict)
        }
    }
    
    private func init_views() -> Void {
        view.addSubview(topImg)
        view.addSubview(segmentSearchView)
    }
}

// MARK: 代理实现
extension VCSearch: SegmentSearchViewsDelegate {
    
    // 到达城市点击
    func search_stopCity() {
        let vc = VCCitySelect()
        vc.title = "选择到达城市"
        vc.complete = {city in
            self.segmentSearchView.resetStopCity = city
        }
        self.pushNextVcHideShowBottomBar(vc: vc)
    }
    
    //  舱位级别
    func search_cabinName() {
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction(title: "公务舱", style: .default, handler: { (action) in
            self.segmentSearchView.resetCabinName = "公务舱"
        }))
        alert.addAction(UIAlertAction(title: "经济舱", style: .default, handler: { (action) in
            self.segmentSearchView.resetCabinName = "经济舱"
        }))
        alert.addAction(UIAlertAction(title: "舱位不限", style: .default, handler: { (action) in
            self.segmentSearchView.resetCabinName = "舱位不限"
        }))
        alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
        present(alert, animated: true, completion: nil)
    }
    
    // 出发城市点击
    func search_StartCity() {
        let vc = VCCitySelect()
        vc.title = "选择出发城市"
        vc.complete = {city in
            self.segmentSearchView.resetStartCity = city
        }
        self.pushNextVcHideShowBottomBar(vc: vc)
    }
    
    // 选择往返日期
    func search_stopSelectDate() {
        let vc = VCDateSelect()
        vc.title = "选择往返日期"
        vc.complete = {date in
            self.segmentSearchView.resetStopCityDate = date
        }
        self.pushNextVcHideBottomBar(vc: vc)
    }
    
    //  选择出发日期
    func search_startSelectDate() {
        let vc = VCDateSelect()
        vc.title = "选择出发日期"
        vc.complete = {date in
            self.segmentSearchView.resetStartCityDate = date
        }
        self.pushNextVcHideBottomBar(vc: vc)
    }
}
